package com.example.springtest.course.controller;


import com.example.springtest.common.R;
import com.example.springtest.course.pojo.entity.Course;
import com.example.springtest.course.service.CourseService;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 课程控制层
 */

@RestController
@RequestMapping("course")
@Slf4j
@Api(tags = "课程")
public class CourseController {
@Autowired
private CourseService courseService;

    /**
     * 查询课程
     * @return
     */
    @GetMapping("list")
    public R<List<Course>> getCourseList(){
    List<Course> courseList=courseService.getCourseList();
    return R.ok(courseList);
}

    /**
     * 增加课程
     * @param course
     * @return
     */
    @PostMapping("add")
    public R<Void> addCourse(@RequestBody @Validated Course course){
        log.debug("增加课程：{}",course);
        courseService.addCourse(course);
        return R.ok();
    }

    /**
     * 删除课程
     * @param id
     * @return
     */
    @DeleteMapping("delete/{id}")
    public R<Void> deleteCourse(@PathVariable Integer id){
        log.debug("删除课程的ID:{}",id);
        courseService.deleteCourse(id);
        return R.ok();
    }

    /**
     * 编辑课程
     * @param id
     * @param course
     * @return
     */
    @PutMapping("edit/{id}")
    public R<Void> editCourse(@PathVariable Integer id,@RequestBody @Validated Course course){
        log.debug("编辑的课程:{}",course);
        courseService.editCourse(id,course);
        return R.ok();
    }
}
